/*
 * Copyright (C), 2002-2016, 苏宁易购电子商务有限公司
 * FileName: ModelCheckController.java
 * Author:   13075787
 * Date:     2016年7月11日 下午12:03:45
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.web.controller.model;

import java.security.Principal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.intf.model.ModelCheckVo;
import com.suning.sawp.intf.model.ModelConstant;
import com.suning.sawp.intf.storeman.StoreManService;
import com.suning.sawp.service.impl.model.ModelBlendServiceImpl;
import com.suning.sawp.service.impl.model.ModelCheckServiceImpl;
import com.suning.sawp.web.controller.DjBaseController;
import com.suning.sawp.web.exception.MyGsonView;

/**
 * 样机管理<br>
 * 盘点
 *
 * @author 13075787
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/model/check")
public class ModelCheckController extends DjBaseController {

    /**
     * 单商品样机总数最大值
     */
    private static final int MODEL_MAX = 99;
    
    @Autowired
    ModelBlendServiceImpl modelBlendService;

    @Autowired
    StoreManService storeManService;

    @Autowired
    ModelCheckServiceImpl modelCheckService;

    /**
     * 功能描述: <br>
     * 查询在盘点的数据
     *
     * @param principal
     * @param taskId
     * @param index
     * @param size
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryCheckList.do")
    public MyGsonView queryModelCheckList(Principal principal, Long taskId, Integer index, Integer size) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg<List<ModelCheckVo>> ret = new ReturnMsg<List<ModelCheckVo>>();
            if (modelBlendService.checkTaskStatus(storeMan, taskId)) {
                ret = modelCheckService.queryModelCheckListForStaff(storeMan, taskId, index, size);
            } else {
                // 活动已失效
                ret.setError(ModelConstant.MODEL_ERROR_TASK_EXPIRE,
                        ModelConstant.getErrorValue(ModelConstant.MODEL_ERROR_TASK_EXPIRE));
            }
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 功能描述: <br>
     * 扫码盘点
     */
    @RequestMapping("/saveScanCheck.do")
    public MyGsonView scanModelCheck(Principal principal, Long taskId, String commCode, String priceId) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg<ModelCheckVo> ret = new ReturnMsg<ModelCheckVo>();
            if (modelBlendService.checkTaskStatus(storeMan, taskId)) {
                ret = modelCheckService.saveModelCheck(storeMan, taskId, commCode, priceId);
            } else {
                // 活动已失效
                ret.setError(ModelConstant.MODEL_ERROR_TASK_EXPIRE,
                        ModelConstant.getErrorValue(ModelConstant.MODEL_ERROR_TASK_EXPIRE));
            }
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 功能描述: <br>
     * 修改盘点样机数量
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping("/saveCheckCommNum.do")
    public MyGsonView updateCheckCommNum(Principal principal, Long taskId, String commCode, Integer commNum) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            if (null == commNum || commNum.intValue() > MODEL_MAX) {
                setErrorMsg(view, ErrorCodeConstants.ERR_0037);
            } else {
                ReturnMsg ret = new ReturnMsg();
                if (modelBlendService.checkTaskStatus(storeMan, taskId)) {
                    ret = modelCheckService.updateCheckCommNum(storeMan, taskId, commCode, commNum);
                } else {
                    // 活动已失效
                    ret.setError(ModelConstant.MODEL_ERROR_TASK_EXPIRE,
                            ModelConstant.getErrorValue(ModelConstant.MODEL_ERROR_TASK_EXPIRE));
                }
                view = setGsonView(ret);
            }
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 功能描述: <br>
     * 修改样机属性
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping("/saveCheckCommAttr.do")
    public MyGsonView updateCheckCommAttribute(Principal principal, Long taskId, String commCode, Integer attribute,
            String actualCommCode) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg ret = new ReturnMsg();
            if (modelBlendService.checkTaskStatus(storeMan, taskId)) {
                ret = modelCheckService.updateCheckCommAttribute(storeMan, taskId, commCode, attribute, actualCommCode);
            } else {
                // 活动已失效
                ret.setError(ModelConstant.MODEL_ERROR_TASK_EXPIRE,
                        ModelConstant.getErrorValue(ModelConstant.MODEL_ERROR_TASK_EXPIRE));
            }
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 功能描述: <br>
     * 删除某条盘点数据
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping("/delCheckComm.do")
    public MyGsonView delCheckComm(Principal principal, Long taskId, String commCode) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg ret = new ReturnMsg();
            if (modelBlendService.checkTaskStatus(storeMan, taskId)) {
                ret = modelCheckService.delCheckModel(storeMan, taskId, commCode);
            } else {
                // 活动已失效
                ret.setError(ModelConstant.MODEL_ERROR_TASK_EXPIRE,
                        ModelConstant.getErrorValue(ModelConstant.MODEL_ERROR_TASK_EXPIRE));
            }
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 功能描述: <br>
     * 盘点提交
     *
     * @param principal
     * @param taskId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping("/submitCheck.do")
    public MyGsonView submitCheck(Principal principal, Long taskId) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg ret = modelCheckService.sumbitCheck(taskId, storeMan);
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

}
